//
//  HMDatePicker.m
//  01-网易彩票
//
//  Created by SZSYKT_iOSBasic_2 on 16/2/20.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "HMDatePicker.h"
#import "HMKeyboardTool.h"

@interface HMDatePicker()
// 工具条
@property (nonatomic, weak) HMKeyboardTool *tool;
// 日期选择器
@property (nonatomic, weak) UIDatePicker *datePicker;
// 遮盖按钮
@property (nonatomic, strong) UIButton *coverBtn;
@end

@implementation HMDatePicker

/**
 *  创建日期选中器
 */
+(instancetype)datePicker {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
         [self setup];
        
    }
    return self;
}

/**
 *  添加子控件
 */
- (void)setup {
    self.backgroundColor = [UIColor whiteColor];
    // 创建工具条
    HMKeyboardTool *tool = [HMKeyboardTool tool];
    // 设置block
    tool.btnClickBlock = ^(HMKeyboardToolButtonType buttonType){
        // 隐藏当前view
        [self dismiss];
        
        if (buttonType == HMKeyboardToolButtonTypeSure)
        {
            // 通知代理
            [self datePickerValueChanged];
        }
    };
    // 将工具条添加到当前view
    [self addSubview:tool];
    self.tool = tool;
    
    // 创建日期选择器
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    // 设置时区
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];

    // 设置时间模式
    datePicker.datePickerMode = UIDatePickerModeDate;
    // 监听日期值改变事件
    
    //UIDatePicker时间范围限制
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:0];//设置最大时间为：当前时间推后十年
    NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps setYear:-100];//设置最小时间为：当前时间前推十年
    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [datePicker setMaximumDate:maxDate];
    [datePicker setMinimumDate:minDate];
    
//    [datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
    
    // 添加到当前view
    [self addSubview:datePicker];
    self.datePicker = datePicker;
    
    [self.tool mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.tool.mas_bottom);
        make.bottom.equalTo(self);
    }];
}

- (void)setCurrentDate:(NSDate *)currentDate {
    _currentDate = currentDate;
    self.datePicker.date = currentDate;
}

/**
 *  监听日期值改变事件
 */
- (void)datePickerValueChanged {
  
    if ([self.delegate respondsToSelector:@selector(datePickerDidValueChanged:)]) {
        [self.delegate datePickerDidValueChanged:self];
    }
}

#pragma mark - 显示和隐藏方法
/**
 *  显示
 */
- (void)show {
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    CGFloat screenW = CGRectGetWidth(window.frame);
    
    // 创建遮盖按钮
    self.coverBtn = [[UIButton alloc] initWithFrame:window.bounds];
    self.coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.5);
    [self.coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    // 将按钮添加到窗口上
    [window addSubview:self.coverBtn];
    
    // 设置当前view的frame
    CGFloat height = 256*K_SCALE_ELSE_TABBAR;
    self.frame = CGRectMake(0, screenH, screenW, height);
    
    // 将当前view添加到窗口上
    [window addSubview:self];
    
    // 以动画的形式显示出来
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH - height;
        self.frame = frame;
    }];
}
/**
 *  隐藏
 */
- (void)dismiss{
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    
    // 将遮盖按钮移除
    [self.coverBtn removeFromSuperview];
    self.coverBtn = nil;
    
    // 以动画的形式隐藏日期选择器
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH;
        self.frame = frame;
    } completion:^(BOOL finished) {
        // 移除当前控件
        [self removeFromSuperview];
        
    }];
}



@end
